import dayjs, { Dayjs } from "dayjs";
import config, { isDev } from "../config";

export function formatNumeric(num: number | string) {
  return +num < 10 ? "0" + num : num;
}
/**
 * @description 添加电话码空格
 * @author created by 孔东
 * @date 2023-07-10 09:47:44
 * @export
 * @param {string} [phone] 电话号码
 * @returns {*}
 */
export function formatPhone(phone?: string) {
  if (!phone) {
    return;
  }
  phone = phone.replace(/\s*/g, "");
  const phones = [
    phone.substr(0, 3),
    phone.substr(3, 4),
    phone.substr(7, 4),
  ].filter(Boolean);
  return phones.join(" ");
}

/**
 * @description 移除电话号码的空格
 * @author created by 孔东
 * @date 2023-07-10 09:47:33
 * @export
 * @param {string} [phone] 电话号码
 * @returns {*} 返回电话号码
 */
export function trimPhone(phone?: string) {
  if (!phone) {
    return "";
  }
  return phone.replace(/\s*/g, "");
}

const assets = Object.values(import.meta.glob("../static/*/**", { eager: true })).map((m:any) => m.default)
/**
 * @description 统一格式化minio 资源文件地址
 * @author created by 孔东
 * @date 2023-07-10 09:45:41
 * @export
 * @param {(string | string[])} assetPath 资源文件地址
 * @returns {*} 返回拼接之后的资源文件地址
 */
export function formatAssetsBaseUrl<T = string | string[]>(assetPath: T): T {
  if (typeof assetPath === "string") {
    // return isDev ? require("/static/" + assetPath) : (config.assetsBaseUrl + assetPath) as T;
    return assets.find((i:string)=> i.endsWith(assetPath))
  } else {
    return (assetPath as string[]).map(
      (path) => config.assetsBaseUrl + path
    ) as T;
  }
}

export type DateType = string | number | Date | Dayjs | null | undefined;
/**
 * @description 格式化日期
 * @author created by 孔东
 * @date 2023-07-17 10:23:35
 * @export
 * @param {(string | number | Date | Dayjs)} [date] 格式化的日期
 * @param {boolean} [useDefaultNow] 是否启用默认当前日期
 * @returns {*}  格式化后的日期
 */
export function formateDateYMD(
  date?: DateType,
  useDefaultNow?: boolean
): string {
  return useDefaultNow
    ? dayjs().format("YYYY-MM-DD")
    : date
    ? dayjs(date).format("YYYY-MM-DD")
    : "";
}

export function formateDateYMDHM(
  date?: DateType,
  useDefaultNow?: boolean
): string {
  return useDefaultNow
    ? dayjs().format("YYYY-MM-DD HH:mm")
    : date
    ? dayjs(date).format("YYYY-MM-DD HH:mm")
    : "";
}